home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5797 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: printing pid_t...
  5. Date: Tue, 20 Feb 96 23:34:04 GMT
  6. Organization: none
  7. Message-ID: <824859244snz@genesis.demon.co.uk>
  8. References: <4gcv6f$6jf@cisunix1.dfci.harvard.edu>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4gcv6f$6jf@cisunix1.dfci.harvard.edu>
  15.            gotd@jimmy.harvard.edu "Godfrey Degamo" writes:
  16.  
  17. >Hello.  Sorry to bother you with this question.  I don't know
  18. >if this belongs in comp.unix.programmers or here.
  19. >
  20. >Anyways, I want to read and write pid_t to a text file.  The problem
  21. >is how is pid_t type defined?  As an int, long, unsigned int or
  22. >unsigned long.  (I think that's all the types that pid_t can be...)
  23. >Can I be safe to assume unsigned long and use %lu for printing and
  24. >scanning?
  25.  
  26. A major reason for creating a type alias such as pid_t is to allow the
  27. implementation to choose the most appropriate type for it. While pid_t
  28. itself is Unix/POSIX specific the problem of printing values such as this
  29. is a more general one. For example the C language defines a type size_t
  30. which is the type of the result of the sizeof operator. size_t is defined
  31. by the standard to be an 'unsigned integral type' but you don't know which.
  32. However you can cast any unsigned integral type to unsigned long while
  33. preserving the value so the portable way of printing the size of a variable
  34. x is:
  35.  
  36.    printf("%lu\n", (unsigned long) sizeof x);
  37.  
  38. Similarly the C language standard defines ptrdiff_t to be "the signed
  39. integral type of the result of subtracting two pointers" so you could
  40. portably write:
  41.  
  42.    printf("%ld\n", (long)(ptr2-ptr1));
  43.  
  44. I don't know if pid_t is defined to be a signed type or not but its usage
  45. in fork() and especially kill() suggests that it won't in practice hold
  46. a value which can't be represented by a long, so I'd use a form equivalent to
  47. the 2nd example. Direct further discussion of pid_t to comp.unix.programmer.
  48.  
  49. -- 
  50. -----------------------------------------
  51. Lawrence Kirby | fred@genesis.demon.co.uk
  52. Wilts, England | 70734.126@compuserve.com
  53. -----------------------------------------
  54.